<?php
/**
 * @api {get} http://aicomm.bndvip.com/Api/IpShow:city Request 天气查询接口
 * @apiName IpShow
 * @apiGroup IpShow
 *
 * @apiParam {Number} city 城市名-必须带上市-县等后缀单位(必填).
 *
 * @apiSuccess {Number} code        返回code(大于0为成功).
 * @apiSuccess {Number} citycode    城市编号.
 * @apiSuccess {String} city        城市名.
 * @apiSuccess {String} winddirect  风向.
 * @apiSuccess {String} windpower   风力.
 * @apiSuccess {String} weather     天气状态.
 * @apiSuccess {String} week        星期.
 * @apiSuccess {String} datetime    日期.
 * @apiSuccess {String} month       农历日期.
 * @apiSuccess {String} quality     空气质量.
 * @apiSuccess {Number} temp        温度.
 *
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/IpShow/advertisement 广告查询
 * @apiName advertisement
 * @apiGroup IpShow
 *
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/IpShow/notice 通知查询
 * @apiName notice
 * @apiGroup IpShow
 *
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/IpShow/hotZhuanti 热门专题查询
 * @apiName hotZhuanti
 * @apiGroup IpShow
 *
 */
namespace app\api\controller;
use app\common\controller\Api;
use \think\Log;

class IpShow extends Api{

      private $host    =  "http://aliv18.data.moji.com";
      private $path    =  "/whapi/json/alicityweather/limit";
      private $method  = "POST";
      private $appcode = "d767805096e84d10afb180e19ad5f254";
    /**
     * 返回天气/城市
     */
    public function index(){
        $result = $this->request->param();
        if (empty($result['city'])){
            $city = '北京市';
        }else{
            $city = $result['city'];
        }
        $result = array();
        //地区code
        $citywhere['name'] = array('eq',$city);
        $citycode   =  M('weather_cityid')->where($citywhere)->find();
        if (empty($citycode)){
            $result['citycode'] = '284609';
        }else{
            $result['citycode'] = $citycode['cityid'];
        }
        //当前时间
        $time = date('Y-m-d',time());
        //查询该城市数据是否存在
        $tqdata = model('Weather')->FindWeather($time,$result['citycode']);
        if (!empty($tqdata)){
            $tqdata['city'] = $city;
            $tqdata['code'] = 3;
            echo json_encode($tqdata);die;
        }else {
            //获取天气
            $data = $this->winddirect($result['citycode']);
            //风向
            $result['winddirect'] = $data['result']['winddirect'];
            //风力
            $result['windpower'] = $data['result']['windpower'];
            //天气
            $result['weather'] = $data['result']['weather'];
            //星期
            $result['week'] = $data['result']['week'];
            //日期
            $result['datetime'] = $data['result']['date'];
            //图片
            $result['img'] = '/public/images/weathercn/'.$data['result']['img'].'.png';
            //阳历转阴历
            if (!empty($result['datetime'])) {
                vendor('dateto.Lunar');
                $lunar = new \Lunar();
                $time = explode('-', $result['datetime']);
                //获取农历
                $month = $lunar->convertSolarToLunar($time[0], $time[1], $time[2]);
                $result['month'] = $month[1].$month[2];
            }
            //空气质量
            $result['quality'] = $data['result']['aqi']['quality'];
            //气温
            $result['temp'] = $data['result']['temp'];
            //存天气数据
            $upadteMysql = model('Weather')->UpdateWeather($result);
            $result['city'] = $city;
            $result['code'] = 3;
            echo json_encode($result);die;
        }
    }

    /**
     * 天气获取
     */
    public function winddirect($city){
        //获取天气
        $host = $this->host;
        $path = $this->path;
        $method = $this->method;
        $appcode = $this->appcode;
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        //根据API的要求，定义相对应的Content-Type
        array_push($headers, "Content-Type".":"."application/x-www-form-urlencoded; charset=UTF-8");
        $querys = "";
        $bodys = "cityId=".$city."&token=50b53ff8dd7d9fa320d3d3ca32cf8ed1";
        $url = $host . $path;

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, true);
        if (1 == strpos("$".$host, "https://"))
        {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);
        $data = json_decode('{"code"'.explode('{"code"', curl_exec($curl))[1],true);
        $kongqi = $this->kongqi($city);
        //风向
        $list['result']['winddirect'] = $data['data']['condition']['windDir'];
        //风力
        $list['result']['windpower'] = $data['data']['condition']['windLevel'];
        //天气
        $list['result']['weather'] = $data['data']['condition']['condition'];
        //日期
        $list['result']['date'] = date('Y-m-d H:i:s',time());
        //星期
        $weekarray=array("日","一","二","三","四","五","六");
        $list['result']['week'] = "星期".$weekarray[date("w",time())];
        //空气质量
        $list['result']['aqi']['quality'] = $kongqi;
        //气温
        $list['result']['temp'] = $data['data']['condition']['temp'];
        //图标
        $list['result']['img'] = $data['data']['condition']['icon'];
        return $list;
    }

    /**
     * 空气指数
     */
    public function kongqi($city){
        $host = "http://aliv18.data.moji.com";
        $path = "/whapi/json/alicityweather/aqi";
        $method = $this->method;
        $appcode = $this->appcode;
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        //根据API的要求，定义相对应的Content-Type
        array_push($headers, "Content-Type".":"."application/x-www-form-urlencoded; charset=UTF-8");
        $querys = "";
        $bodys = "cityId=".$city."&token=8b36edf8e3444047812be3a59d27bab9";
        $url = $host . $path;
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, true);
        if (1 == strpos("$".$host, "https://"))
        {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);
        $data = json_decode('{"code"'.explode('{"code"', curl_exec($curl))[1],true);
        $line = $data['data']['aqi']['value'];
        if ($line<0){
            $text = '其他';
        }elseif ($line <=50 && $line >=0){
            $text = '优';
        }elseif ($line <=100 && $line >=51){
            $text = '良';
        }elseif ($line <=150 && $line >=101){
            $text = '轻度污染';
        }elseif($line <=200 && $line >=151){
            $text = '中度污染';
        }elseif($line <=300 && $line >=201){
            $text = '重度污染';
        }elseif($line <=500 && $line >=301){
            $text = '严重污染';
        }else{
            $text = '良';
        }
        return $text;
    }

    /**
     * 获取广告
     */
    public function advertisement(){
        $where['end_time'] = array('gt',time());
        $where['ad_top'] = array('eq',1);
        $where['ad_status'] = array('eq',1);
        $data = M('ad_text')->where($where)->select();
        if ($data){
            $return = codemsg(3);
            $return['data'] = $data;
            echo json_encode($return);die;
        }else{
            $return = codemsg(-7028);
            echo json_encode($return);die;
        }
    }

    /**
     * 获取通知
     */
    public function notice(){
        $where['is_top'] = array('eq',1);
        $where['status'] = array('eq',0);
        $data = M('notice')->where($where)->select();
        if ($data){
            $return = codemsg(3);
            $return['data'] = $data;
            echo json_encode($return);die;
        }else{
            $return = codemsg(-7028);
            echo json_encode($return);die;
        }
    }

    //首页的热门专题
    public function hotZhuanti(){
        //查询是否置顶
        $where['top'] = array('eq',1);
        $where['is_zhuanti'] = array('eq',1);
        $where['recycle'] = array('eq',0);
        $where['is_del'] = array('eq',0);
        $list = M('dis_post')->where($where)->select();
        if ($list){
            $return = codemsg(3);
            $return['data'] = $list;
            echo json_encode($return);die;
        }else{
            $return = codemsg(-7028);
            echo json_encode($return);die;
        }
    }

}